import service from "../../utils/request"

// components/carnumber/carnumber.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    carnumner: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    areas:[
      [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" ],
      [ "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P" ],
      [ "A", "S", "D", "F", "G", "H", "J", "K", "L" ],
      [ "Z", "X", "C", "V", "B", "N", "M", "-" ],
    ],
    provinces:[
      [ "京", "津", "沪", "渝", "川", "新", "藏", "宁", "桂", "贵" ],
      [ "云", "黑", "吉", "辽", "晋", "冀", "青", "鲁", "豫", "苏" ],
      [ "皖", "浙", "闽", "赣", "湘", "鄂", "粤", "琼", "甘", "陕" ],
      [ "蒙", "港", "澳", "台", "使", "领", "警", "学", "=" ],
    ],
    type: 'provinces',
    length:8,
    fill:new Array(8).fill(''),
    current: 0,
    focus:true,
    deleteKeys: ['-', '='],
  },
  created(){
    console.log('carnumner',this.data.fill)
  },
  attached() {
    
    if(this.data.carnumner.length ==7){

    }
    
    this.changeType()
  },
  observers: {
    'current': function(current) {
      var curType =current === 0 ? 'provinces' : 'areas'
      console.log('当前ca',current,curType)
      this.setData({
        type: curType
      })
      this.changeType()
    },
    'carnumber': function(carnumber) {
      console.log('当前carnumber',carnumber)
      // this.setData({
      //   type: curType
      // })
      // this.changeType()
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setCar(val) {
      console.log('setCar')
      if(val){
        var data = []
        for(let i=0; i< 8; i++){
          if(!i){
            i = ''
          }
          var is=  val.charAt(i)
          data.push(is)
        }
        
        this.setData({
          fill: data,
          current: 7
        })
      }
    },
    keyHideHandler() {
      this.setData({
        focus: false
      })
    },
    changeType () {
      let data = this.data[this.data.type]
      // console.log('当前car类型',this.data.type,data, this.data.fill)
      this.setData({
        keys: data
      })
    },
    editNumber() {
      var detail =''
      let fills = this.data.fill.map(item => {
       return detail += item
      })
      console.log(detail)
      this.triggerEvent('confirms', detail)
    },
    fopcusHandler(e) {
      var index = e.currentTarget.dataset.index
      if (!index) { index = 0}
      this.setData({
        focus: true,
        current: index
      })
      console.log(this.data.current,e);
    },
    keyTapHandler(e) {
      var key = e.currentTarget.dataset.key
      if (this.data.deleteKeys.includes(key)) {
        this.keyDeleteHandler();
        return;
      }
      this.keyInputHandler(key);
    },
    // input
    keyInputHandler(key) {
      var temp= "fill[" + this.data.current + "]"
      this.setData({
        [temp]: key
      })
      if (this.data.current >= this.data.length - 1) {
        return;
      }
      var cur =  this.data.current += 1;
      this.setData({
        current: cur
      })
    },
    keyDeleteHandler() {
      var temp= "fill[" + this.data.current + "]"
      var cur =this.data.current ===0? 0 :  this.data.current -= 1;
      this.setData({
        [temp]: '',
        current: cur
      })
    },
  }
})
